//
//  YJYPhotoCell.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/11/1.
//  Copyright © 2018 BaizYi. All rights reserved.
//

#import "YJYPhotoCell.h"

#define SCREENWIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREENHEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface YJYPhotoCell ()

@property(strong,nonatomic) UIImageView  *photoView;


@end

@implementation YJYPhotoCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.browserImageScrollView];
    }
    return self;
}

-(void)setImage:(UIImage *)image{
    _image = image;
    
    UIImageView *imageView = _browserImageScrollView.zoomImageView;
    
    imageView.image = image;
    CGSize size = [self backImageSize:image];
    _browserImageScrollView.contentSize = CGSizeMake(size.width, size.height);
    _browserImageScrollView.contentOffset = CGPointMake(0, 0);
    
    CGFloat imageY = (_browserImageScrollView.frame.size.height - size.height) * 0.5;
    imageY = imageY > 0 ? imageY : 0;
    _browserImageScrollView.zoomImageView.frame = CGRectMake(0, imageY, _browserImageScrollView.frame.size.width, size.height);
    
    
}


-(YJYBrowserScrollView *)browserImageScrollView{
    if (_browserImageScrollView == nil) {
        _browserImageScrollView = [[YJYBrowserScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
        _browserImageScrollView.backgroundColor = [UIColor blackColor];
        _browserImageScrollView.zoomScale = 1.0f;
        __weak typeof(self) weakSelf = self;
        _browserImageScrollView.tapBlock = ^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(imageViewClick:)]) {
                [weakSelf.delegate imageViewClick:weakSelf.cellIndex];
            }
        };
    }
    return _browserImageScrollView;
}

-(UIImageView *)photoView{
    if (_photoView == nil) {
        _photoView = [[UIImageView alloc]initWithFrame:self.contentView.frame];
        _photoView.contentMode = UIViewContentModeScaleAspectFill;
        _photoView.layer.masksToBounds = YES;
        _photoView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
        [_photoView addGestureRecognizer:tap];
    }
    return _photoView;
}
-(void)onTap:(UIGestureRecognizer *)tap{
    if (self.block) {
        self.block((UIImageView *)tap.view);
    }
}

-(CGSize)backImageSize:(UIImage *)image{
    CGSize size = image.size;
    CGSize newSize;
    newSize.width = SCREENWIDTH;
    newSize.height = newSize.width / size.width * size.height;
    return newSize;
}

// 获取指定视图在window中的位置
- (CGRect)getFrameInWindow:(UIView *)view
{
    return [view convertRect:view.bounds toView:[UIApplication sharedApplication].keyWindow];
}



@end
